//
// Created by Jisam on 01/10/2024 7:37 PM.
// Solution of  c2
#include <bits/stdc++.h>

#define  int long long
#define  endl "\n"
using namespace std;

struct node {
    int i, j, k;
};

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int T = 1;
    cin >> T;
    while (T--) {
        string s;
        cin >> s;
        int n = s.size();
        vector<node> a;
        map<char, int> mp;
        for (int i = 0; i < n; i++) {
            mp[s[i]]++;
        }
        int cnt1 = 1e9;
        for (auto [x, y]: mp) {
            cnt1 = min(cnt1, y);
        }
        vector<int> vis(n);
        int cnt2 = 0;
        int ok = 0;
        for (int i = 0; i < n && !ok; i++) {
            if (s[i] != 'A' || vis[i]) {
                continue;
            }
//            cout << "i " <<i << endl;
            for (int j = i + 1; j < n && !ok; j++) {
                if (s[j] != 'P' || vis[j]) {
                    continue;
                }
//                cout << "j " << j << endl;

                for (int k = j + 1; k < n && !ok; k++) {
                    if (s[k] != 'C' || vis[k]) {
                        continue;
                    }
                    s[i] = ' ';
                    s[j] = ' ';
                    s[k] = ' ';
                    vis[i] = 1;
                    vis[j] = 1;
                    vis[k] = 1;
                    a.push_back({i, j, k});
                    i ++;
//                    cout << i << " " << j << " " << k << " " << endl;
                    cnt2++;
                    if (cnt2 == cnt1) ok = 1;
//                    cout << "k  " << k << endl;

                }

            }
        }

        int flag = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == ' ')continue;
            flag = 1;
            cout << s[i];
        }
        if(!flag) cout << "Perfect";
        cout << endl;
        cout << a.size() << endl;
        for (int i = 0; i < a.size(); i++) {
            cout << a[i].i  + 1<< " " << a[i].j  + 1<< " " << a[i].k  + 1<< " " << endl;
        }
    }

    return 0;
}